package com.zds.hj.poseidon.biz.dispatcher.credit;

import com.zds.boot.common.context.ZDSContext;
import com.zds.boot.common.exception.BusinessException;
import com.zds.boot.common.utils.Ids;
import com.zds.boot.dispatch.annotation.DispatcherHandler;
import com.zds.boot.dispatch.comm.enums.ModuleNodeTypeEnum;
import com.zds.boot.dispatch.dto.BaseReqModel;
import com.zds.boot.dispatch.dto.BaseResultModel;
import com.zds.boot.dispatch.dto.ModuleBaseInfo;
import com.zds.hj.crm.facade.dto.customer.resp.EntCustomerBusinessRespDto;
import com.zds.hj.icmc.facade.auth.assetcenter.dto.req.LegalRepresentativeVerifyReqDto;
import com.zds.hj.icmc.facade.auth.assetcenter.dto.resp.AuthRespDto;
import com.zds.hj.icmc.facade.auth.assetcenter.dto.resp.LegalRepresentativeVerifyRespDto;
import com.zds.hj.icmc.facade.auth.assetcenter.enums.AuditStatusEnum;
import com.zds.hj.poseidon.biz.cba.hpp.domain.credit.service.layout.CreditApplyService;
import com.zds.hj.poseidon.biz.common.CrmServiceInterface;
import com.zds.hj.poseidon.biz.common.IcmcServiceInterface;
import com.zds.hj.poseidon.facade.dto.hpp.credit.CreditInfoEntityDto;
import com.zds.hj.product.facade.constant.ApplicationContainerConstant;
import com.zds.zly.common.enums.HjResultCodeEnum;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;

/**
 * @Author:hfy
 * @Date:2023/06/06/16:32
 * @Description:
 */
@DispatcherHandler(value = "AuthEntLegalCheckHandler",desc = "企业法人关系检查",
applicationContainer = ApplicationContainerConstant.FRONT_END,nodeType = ModuleNodeTypeEnum.BUSINESS)
@Slf4j
public class AuthEntLegalCheckHandler extends CreditCheckAbsHandler{
    @Resource
    private CrmServiceInterface crmServiceInterface;
    @Resource
    private CreditApplyService creditApplyService;
    @Resource
    private IcmcServiceInterface icmcServiceInterface;

    @Override
    public void creditDoExecute(BaseReqModel params, BaseResultModel resultModel, CreditInfoEntityDto reqData,
                                CreditInfoEntityDto dbData) {
        log.info("=============执行企业法人关系校验模块==============");
        CreditInfoEntityDto creditOnTransit = creditApplyService.findCreditOnTransit(params);
        log.info("查询企业工商信息");
        EntCustomerBusinessRespDto entBusinessRespDto =
                crmServiceInterface.findByEntId(creditOnTransit.getEnterpriseId());
        if (entBusinessRespDto == null || entBusinessRespDto.getBasicDto() == null){
            throw new BusinessException("获取企业工商信息为空", HjResultCodeEnum.AUTH_ERR.code());
        }
        LegalRepresentativeVerifyReqDto legalRepresentativeVerifyReqDto = new LegalRepresentativeVerifyReqDto();
        //社会信用统一代码
        legalRepresentativeVerifyReqDto.setCertificateNo(creditOnTransit.getCompanySocialCredit());
        legalRepresentativeVerifyReqDto.setPersonCardNo(creditOnTransit.getCardId());
        legalRepresentativeVerifyReqDto.setThirdSerialNo(Ids.gid());
//        legalRepresentativeVerifyReqDto.setSerialNo(dbData.getApplyNo());
        legalRepresentativeVerifyReqDto.setChannelNo(params.getChannelNo());
        legalRepresentativeVerifyReqDto.setMerchantNo(params.getMerchantNo());
        AuthRespDto<LegalRepresentativeVerifyRespDto> legalAuthRespDto =
                icmcServiceInterface.legalRepresentativeVerifyRespDtoAuthRespDto(legalRepresentativeVerifyReqDto);
        log.info("企业与法人关系校验结果{}",legalAuthRespDto);
        //校验成功
        if (legalAuthRespDto.getAuditStatusEnum().equals(AuditStatusEnum.AUDIT_PASSED)){
            log.info("企业与法人关系校验通过");
        }else if (legalAuthRespDto.getAuditStatusEnum().equals(AuditStatusEnum.AUDIT_FAILD)){
            log.info("企业与法人关系校验失败");
            throw new BusinessException("企业与法人关系校验失败",AuditStatusEnum.AUDIT_FAILD.code());
        }else{
            throw new BusinessException("企业与法人关系校验超时",AuditStatusEnum.AUDIT_EXPIRE.code());
        }
    }

    @Override
    public ModuleBaseInfo moduleBaseInfo() {
        ModuleBaseInfo moduleBaseInfo = super.moduleBaseInfo();
        moduleBaseInfo.setAuto(true);
        return moduleBaseInfo;
    }

}
